package com.vladimirkolarevic.eventplanner.tapestry.pages;

import java.text.DateFormat;
import java.text.Format;
import java.util.List;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.BeginRender;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.vladimirkolarevic.eventplanner.Attend;
import com.vladimirkolarevic.eventplanner.DateAndTime;
import com.vladimirkolarevic.eventplanner.Event;
import com.vladimirkolarevic.eventplanner.User;
import com.vladimirkolarevic.eventplanner.beans.AttendBean;
import com.vladimirkolarevic.eventplanner.beans.EventBean;
import com.vladimirkolarevic.eventplanner.service.AttendService;
import com.vladimirkolarevic.eventplanner.service.EventsService;
import com.vladimirkolarevic.eventplanner.service.MembershipsService;

public class AllEvents {

	@ApplicationState
	private User user;
	private boolean userExists;

	private List<Event> events;

	private Event event;

	private DateAndTime date;

	private boolean notAttending;

	@Inject
	private EventsService eventsService;

	@Inject
	private MembershipsService membership;

	@Inject
	private AttendService attendService;

	@InjectPage
	private EventInfo eventInfo;

	@SetupRender
	public void retriveEvents() {
		System.out.println("begin");
		events = eventsService.retrive();
		notAttending = true;

	}

	public List<Event> getAllEvents() {
		return events;
	}

	public void setEvent(Event event) {
		System.out.println(event.getId());
		notAttending = true;
		this.event = event;

	}

	public Event getEvent() {

		if (userExists) {
			Attend attend = attendService.retrive(user);
			if (attend != null) {

				List<Event> events = attend.getEvents();
				if (!events.isEmpty()) {
					for (int i = 0; i < events.size(); i++) {

						if (events.get(i).getId().equals(this.event.getId())) {

							notAttending = false;
						}

					}
				}
			}
		}
		return event;
	}

	public boolean isNotAttending() {

		System.out.println("attend");

		return notAttending;
	}

	public Format getDateFormat() {
		return DateFormat.getDateInstance();
	}

	@OnEvent(component = "eventLink")
	public Object showEventInfo(Long id) {
		eventInfo.setId(id);
		return eventInfo;
	}

	@OnEvent(component = "attend")
	public Object attend(Long id) {

		Event event = eventsService.retriveEvent(id);

		Attend at = attendService.retrive(user);

		if (at == null) {
			System.out.println("neshto");
			Attend attend = new AttendBean();

			attend.setUser(user);
			attend.addEvent(event);

			Long id1 = attendService.save(attend);

		} else {

			at.addEvent(event);
			attendService.update(at);
		}

		return AllEvents.class;
	}

	public boolean isUserExists() {
		return userExists;
	}

}
